/**
 * 登陆
 */
var loginController={
    init:function(){
        loginController.setListeners();
    },
    clickLogin:function(){
        var username = $("#loginForm #username").val();
        var password = $("#loginForm #password").val();
        if(username && password){
            $.ajax({
                type: "post",
                url: "/login/checkAdminLogin",
//                cache:false,
//                async:false,
                dataType: "json",
                data:{
                    userName:username,
                    password:password,
                    loginType:1
                },
                success: function(data){
                    if(data == 0){
                        alert("请输入用户名或密码");
                    }else if(data == 2){
                        alert("用户名或密码错误");
                    }else if(data == 1){
                        window.location.href="/custom/product/page/list";
                    }
                },
                failure:function (result) {
                    alert('登陆失败');
                }
            });
        }else{
            alert("请输入用户名或密码");
        }
    },
    setListeners:function(){
        $( "#loginForm" ).validate( {
            submitHandler : function(form) {
                loginController.clickLogin();
            },
            rules: {
                username: "required",
                password: "required"
            },
            messages: {
                username: "请输入用户名",
                password: "请输入密码"
            },
            //errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element.parent() );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parent().next().addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parent().next().addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
}
/**
 * 注册
 */
var registerController={
    init:function(){
        registerController.setListeners();
        registerController.validatorMethod();
    },
    clickRegister:function(){
        var username = $("#registerForm #username_r").val();
        var password = $("#registerForm #password_r").val();
        if(username && password){
            $.ajax({
                type: "post",
                url: "/user/register",
//                cache:false,
//                async:false,
                dataType: "json",
                data:{
                    username:username,
                    password:password,
                    type:1
                },
                success: function(data){
                    console.log(data == 2);
                    if(data == 2){
                        alert("注册成功");
                    }else if(data == 1){
                        // window.location.href="/custom/product/page/list";
                    }
                },
                failure:function (result) {
                    alert('注册失败');
                }
            });
        }else{
            alert("请输入用户名或密码");
        }
    },
    setListeners:function(){
        $( "#registerForm" ).validate( {
            submitHandler : function(form) {
                registerController.clickRegister();
            },
            rules: {
                username_r: {
                    required:true,
                    checkUserNameIsExists:true
                },
                password_r: "required",
                confirm_password: {
                    required: true,
                    // minlength: 5,
                    equalTo: "#password_r"
                },
            },
            messages: {
                username_r: "请输入用户名",
                password_r: "请输入密码",
                confirm_password: {
                    required: "请输入确认密码",
                    // minlength: "Your password must be at least 5 characters long",
                    equalTo: "请输入相同的确认密码"
                },
            },
            //errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element.parent() );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parent().next().addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parent().next().addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    },
    validatorMethod:function(){
        $.validator.addMethod("checkUserNameIsExists", function(value, element) {
            var flag;
            var username = $("#registerForm #username_r").val();
            flag = $.ajax({
                "dataType" : 'text',
                "type" : "POST",
                "async" : false,
                "url" : "/user/register/checkusername",
                "data" : {
                    username : username
                },
                "success" : function(data) {
                }
            }).responseText;
            return this.optional(element) || flag == true;
        }, "用户名已经存在");
    },
}
$(function(){
    loginController.init();
    registerController.init();
});